package cn.tjd.smart_health.common.util;

import cn.tjd.smart_health.common.entity.ReserveSetting;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class ExcelUtil {



    public static  List<ReserveSetting> getInstance(File file){
        List<ReserveSetting> list = new ArrayList<ReserveSetting>();

        try {
            FileInputStream fis = new FileInputStream(file);
            Workbook wb = new XSSFWorkbook(fis);
            Sheet sheet = wb.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();

            rowIterator.next();

            while (rowIterator.hasNext()){
                Row row = rowIterator.next();

                Cell orderDateCell = row.getCell(0);
                Cell orderNum = row.getCell(1);

                if (orderDateCell != null && orderNum != null) {
                    Date date = orderDateCell.getDateCellValue();
                    double num =orderNum.getNumericCellValue();
                    ReserveSetting reserveSetting = new ReserveSetting();

                    LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();


                    reserveSetting.setOrderDate(localDate);
                    reserveSetting.setNumber((int) num);
                    list.add(reserveSetting);
                }

            }

            return list;


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
